export function dmFileByAxios(response) {
  let fileName = ''
  let content = response.headers['content-disposition']
  if (content) {
    content = content.split('; ')
    let key = 'filename='
    for (const item of content) {
      let i = item.indexOf(key)
      if (i !== -1) {
        fileName = item.substring(i + key.length)
        break
      }
    }
  } else {
    fileName = Date.now() + '.xls'
  }
  if (!fileName) {
    throw Error('fileName is error!')
  }
  const url = window.URL.createObjectURL(
    new Blob([response.data], { type: response.headers['content-type'] })
  )
  const link = document.createElement('a')
  link.href = url
  link.setAttribute('download', decodeURI(fileName))
  link.click()
  window.URL.revokeObjectURL(url)
}

export function isImgUrl(url) {
  return url && url.match(/\.(jpeg|jpg|gif|png)$/) != null
}

export function isVideoUrl(url) {
  return url && url.match(/\.(mp4|flv|3gp|mkv|m4a)$/) != null
}

export function isAudioUrl(url) {
  return url && url.match(/\.(mp3|ogg)$/) != null
}

export function createImgObj(response) {
  if (!response || !response.data) {
    return
  }
  const urlCreator = window.URL || window.webkitURL
  return urlCreator.createObjectURL(response.data)
}
